/*
 * Copyright 2012 Alain Pannetrat (L1L1@gmx.com)
 * 
 * This file is part of cookie-miner.
 * 
 * cookie-miner is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * cookie-miner is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with cookie-miner. If not, see http://www.gnu.org/licenses/.
 *
 * @version: $Id$
 *
 */
#include "gfx_widgets.h"
#include "log_facility.h"

gfx::Style gfx::default_style;

/** IMPLEMENTATION **/
bool gfx::Widget::init()
{
	return true;
}

bool gfx::Widget::draw()
{
	return true;
}

/** IMPLEMENTATION **/
bool gfx::Container::init()
{
	for (unsigned i=0;i<_children.size();i++)
	{
		_children[i]->init();
	}
	return true;
}

bool gfx::Container::draw()
{
	for (unsigned i=0;i<_children.size();i++)
	{
		_children[i]->draw();
	}
	return true;
}


bool gfx::Container::on_release(const Event& ev) 
{ 
	Event evr = ev;

	for (unsigned i=0;i<_children.size();i++)
	{
		Widget *child = _children[i];
		const Rectangle& rect = child->position(); 

		if (rect.is_inside(ev.button.x,ev.button.y))
		{
			evr.button.x = ev.button.x - rect.left();
			evr.button.y = ev.button.y - rect.top();
			if (child->on_release(evr)) return true;
		}
	}
	return false; 
}

bool gfx::Container::on_click(const Event& ev) 
{ 
	Event evr = ev;

	for (unsigned i=0;i<_children.size();i++)
	{
		Widget *child = _children[i];
		const Rectangle& rect = child->position(); 

		if (rect.is_inside(ev.button.x,ev.button.y))
		{
			evr.button.x = ev.button.x - rect.left();
			evr.button.y = ev.button.y - rect.top();
			if (child->on_click(evr)) return true;
		}
	}
	return false; 
}

bool gfx::Container::on_double_click(const Event& ev) 
{ 
	Event evr = ev;

	for (unsigned i=0;i<_children.size();i++)
	{
		Widget* child = _children[i];
		const Rectangle& rect = child->position(); 

		if (rect.is_inside(ev.button.x,ev.button.y))
		{
			evr.button.x = ev.button.x - rect.left();
			evr.button.y = ev.button.y - rect.top();
			if (child->on_double_click(evr)) return true;
		}
	}
	return false; 
}

bool gfx::Container::on_mouse_move(const Event& ev) 
{ 
	Event evr = ev;

	for (unsigned i=0;i<_children.size();i++)
	{
		Widget *child = _children[i];
		const Rectangle& rect = child->position(); 

		if (rect.is_inside(ev.motion.x,ev.motion.y))
		{
			evr.motion.x = ev.motion.x - rect.left();
			evr.motion.y = ev.motion.y - rect.top();
			if (child->on_mouse_move(evr)) return true;
		}
	}
	return false; 
}

bool gfx::Container::on_key_press(const Event& ev) 
{ 
	Event evr = ev;

	for (unsigned i=0;i<_children.size();i++)
	{
		Widget *child = _children[i];

		if (child->on_key_press(evr)) return true;
	}
	return false; 
}

bool gfx::Container::resize() 
{ 
	for (unsigned i=0;i<_children.size();i++)
	{
		_children[i]->resize();
	}
	return true; 
}

